package com.doupi.partner.service.impl;

import com.doupi.partner.dal.AdministratorRoleMapper;
import com.doupi.partner.dal.constant.DalConstants;
import com.doupi.partner.dal.model.AdministratorRole;
import com.doupi.partner.dal.model.AdministratorRoleHasPermission;
import com.doupi.partner.service.AdministratorHasRoleService;
import com.doupi.partner.service.AdministratorRoleHasPermissionService;
import com.doupi.partner.service.AdministratorRoleService;
import com.doupi.partner.service.dto.AdministratorRoleDto;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdministratorRoleServiceImpl implements AdministratorRoleService {
	private final AdministratorRoleMapper administratorRoleMapper;

	private final AdministratorRoleHasPermissionService administratorRoleHasPermissionService;

	private final AdministratorHasRoleService administratorHasRoleService;

	@Autowired
	public AdministratorRoleServiceImpl(AdministratorRoleMapper administratorRoleMapper, AdministratorRoleHasPermissionService administratorRoleHasPermissionService,
			AdministratorHasRoleService administratorHasRoleService) {
		this.administratorRoleMapper = administratorRoleMapper;
		this.administratorRoleHasPermissionService = administratorRoleHasPermissionService;
		this.administratorHasRoleService = administratorHasRoleService;
	}

	public int deleteByPrimaryKey(Long id) {
		int flag = administratorRoleMapper.deleteByPrimaryKey(id);
		if (flag > 0) {
			// 删除管理员-角色对应关系
			administratorHasRoleService.deleteByAdministratorRoleId(id);
			// 删除角色-权限对应关系
			administratorRoleHasPermissionService.deleteByAdministratorRoleId(id);
		}
		return flag;
	}

	public int insert(AdministratorRole record) {
		return administratorRoleMapper.insert(record);
	}

	public AdministratorRole selectByPrimaryKey(Long id) {
		return administratorRoleMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(AdministratorRole record) {
		return administratorRoleMapper.updateByPrimaryKeySelective(record);
	}

	public int countAdministratorRole(AdministratorRole administratorRole) {
		return administratorRoleMapper.countAdministratorRole(administratorRole);
	}

	public List<AdministratorRole> queryAdministratorRoles(AdministratorRole administratorRole, RowBounds rowBounds) {
		return administratorRoleMapper.queryAdministratorRoles(administratorRole, rowBounds);
	}

	public AdministratorRole selectAdministratorRoleForUpdateByPrimaryKey(Long id) {
		return administratorRoleMapper.selectAdministratorRoleForUpdateByPrimaryKey(id);
	}

	public Pagination<AdministratorRole, AdministratorRole> queryAdministratorRoles(Pagination<AdministratorRole, AdministratorRole> pagination) {
		int count = countAdministratorRole(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryAdministratorRoles(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public void saveAdministratorRole(AdministratorRoleDto administratorRoleDto) {
		int flag;
		// 1.更新或插入角色
		if (administratorRoleDto.getId() == null || administratorRoleDto.getId() <= 0) {
			flag = insert(administratorRoleDto);
		} else {
			flag = updateByPrimaryKeySelective(administratorRoleDto);
		}

		// 2.更新或插入成功删除之前的角色配置关系
		if (flag > 0) {
			administratorRoleHasPermissionService.deleteByAdministratorRoleId(administratorRoleDto.getId());
		}

		// 3.更新或插入成功重新插入角色配置关系
		if (flag > 0 && null != administratorRoleDto.getAdministratorRoleHasPermissions()) {
			for (AdministratorRoleHasPermission administratorPermission : administratorRoleDto.getAdministratorRoleHasPermissions()) {
				administratorPermission.setAdministratorRoleId(administratorRoleDto.getId());
				administratorRoleHasPermissionService.insert(administratorPermission);
			}
		}
	}

	@Override
	public AdministratorRole selectAdministratorRoleByCode(String code) {
		AdministratorRole administratorRole = new AdministratorRole();
		administratorRole.setCode(code);
		List<AdministratorRole> list = queryAdministratorRoles(administratorRole, DalConstants.SINGLE_ROW_BOUNDS);
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
}
